﻿//-----------------------------------------------------------------------
// <copyright file="SlwyController.cs" company="SLWY Enterprises">
// * Copyright (C) 2017 四川商旅无忧科技有限公司 版权所有。
// * version : 1.0
// * author  : qinchaoyue
// * FileName: SlwyController.cs
// * history : created by qinchaoyue 2017-10-22 11:09:31
// </copyright>
//-----------------------------------------------------------------------

using System;

using Microsoft.AspNetCore.Mvc;

using Slwy.Context;
using Slwy.MVC.Models;
using Operator = Slwy.Mvc.Models.Operator;

namespace Slwy.Mvc.Controller
{
    /// <summary>
    /// 定义返回方法, 包装一些返回参数
    /// </summary>
    public class SlwyController : ControllerBase
    {
        /// <summary>
        /// 自定义返回结果
        /// </summary>
        /// <param name="result">返回结果</param>
        /// <returns>请求结果</returns>
        [NonAction]
        [Obsolete("请勿使用")]
        public OkObjectResult Custome(MSlwyResult result)
        {
            return base.Ok(result);
        }

        /// <summary>
        /// 自定义返回结果
        /// </summary>
        /// <param name="obj">结果</param>
        /// <returns>请求结果</returns>
        [NonAction]
        [Obsolete("请勿使用")]
        public OkObjectResult Custome(object obj)
        {
            return base.Ok(obj);
        }

        /// <summary>
        /// 返回失败的请求
        /// 返回 code = 0
        /// </summary>
        /// <param name="data">失败数据</param>
        /// <param name="message">失败描述</param>
        /// <returns>失败的请求</returns>
        [NonAction]
        public OkObjectResult Error(object data, string message)
        {
            return base.Ok(new MSlwyResult
            {
                Code = 0,
                Message = message,
                Data = data,
            });
        }

        /// <summary>
        /// 返回失败的请求
        /// 返回 code = 0
        /// </summary>
        /// <param name="message">失败信息.</param>
        /// <returns>失败的请求</returns>
        [NonAction]
        public OkObjectResult Error(string message)
        {
            return base.Ok(new MSlwyResult { Code = 0, Message = message, });
        }

        /// <summary>
        /// 返回失败的请求
        /// 返回 code = 0
        /// </summary>
        /// <param name="e">异常</param>
        /// <returns>失败的请求</returns>
        [NonAction]
        public OkObjectResult Error(Exception e)
        {
            return base.Ok(new MSlwyResult
            {
                Code = 0,
                Message = e.Message,
                Data = e
            });
        }

        /// <summary>
        /// 返回成功的请求
        /// 返回 code = 1
        /// </summary>
        /// <param name="value">数据</param>
        /// <returns>成功</returns>
        [NonAction]
        public override OkObjectResult Ok(object value)
        {
            var result = new MSlwyResult
            {
                Code = 1,
                Data = value,
            };
            return base.Ok(result);
        }

        /// <summary>
        /// 返回成功的请求
        /// 返回 code = 1
        /// </summary>
        /// <returns>成功</returns>
        [NonAction]
        public OkObjectResult Ok()
        {
            return base.Ok(new MSlwyResult
                               {
                                   Code = 1,
                                   Data = string.Empty,
                                   Message = "请求成功",
                               });
        }

        /// <summary>
        /// 返回成功的请求
        /// 返回 code = 1
        /// </summary>
        /// <param name="value">数据</param>
        /// <param name="message">描述</param>
        /// <returns>成功</returns>
        [NonAction]
        public OkObjectResult Ok(object value, string message)
        {
            return base.Ok(new MSlwyResult
            {
                Code = 1,
                Data = value,
                Message = message,
            });
        }

        /// <summary>
        /// 获取 token
        /// </summary>
        public string Token => SlwyContext.Current.Token;

        /// <summary>
        /// 获取 当前操作者
        /// </summary>
        public Operator Operator => SlwyContext.Current.Operator;
    }
}
